package com.qding.park.manager.rest.controller;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.common.web.controller.BaseController;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.common.TerminalStatisFacade;
import com.qding.park.manager.pojo.TerminalClickInfo;

/**
 * @author Administrator 终端日志记录事件
 */
@RestController
public class TerminalController extends WebBaseController {

	private static final Log _log = LogFactory.getLog(TerminalController.class);
	
	
	@Autowired
	private   TerminalStatisFacade  terminalStatisFacade;
	

	/**
	 * @param OSType 终端系统(1:android,2:iOS)
	 * @param OSVersion 终端系统版本号
	 * @param buttonType 按钮(1：车位预定 2：缴费 3：车辆邀请 4：代客缴费 5：锁车 6：记录)
	 * @param phone  手机号
	 * @param phoneModel  手机型号
	 * @param projectId 项目号
	 * @param userId  用户id
	 * @return
	 */
	@RequestMapping("/log/terminalClick")
	public AnswerMsg getParkings(String OSType, String OSVersion, String buttonType, String phone, String phoneModel,
			String projectId, String userId,String parkId) {
			
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("OSType", OSType);
			vb.getRequiredValidate().put("OSVersion", OSVersion);
			vb.getRequiredValidate().put("buttonType", buttonType);
			vb.getPhoneValidate().put("phone", phone);
			vb.getRequiredValidate().put("phoneModel", phoneModel);
			vb.getRequiredValidate().put("projectId", projectId);
			vb.getRequiredValidate().put("userId", userId);
			
			vb.getIntValidate().put("OSType", OSType);
			vb.getIntValidate().put("buttonType", buttonType);
			RchlValidateTools.validate(vb);
			/**************/
			
			TerminalClickInfo  clickInfo=new TerminalClickInfo();
			clickInfo.setButtonType(Integer.valueOf(buttonType));
			clickInfo.setOSType(Integer.valueOf(OSType));
			clickInfo.setOSVersion(OSVersion);
			clickInfo.setPhone(phone);
			clickInfo.setProjectId(projectId);
			clickInfo.setUserId(userId);
			clickInfo.setParkId(parkId);
			clickInfo.setPhoneModel(phoneModel);
			clickInfo.setCreateDate(new Date());
			
//			ParkDataResult dataResult = parkFacade.getParkings(userId, Long.valueOf(phone), projectId);
			
			terminalStatisFacade.terminalClick(clickInfo);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(null);
			return answerMsg;
		} catch (RchlValidatorException e) {
			exceptionHandle.handle(e);// 异常处理
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
					"未知Exception", "userId=" + userId + ",phone=" + phone));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}
}
